package com.gqj.meter.service.impl;

import com.gqj.common.constant.UserConstants;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.bean.BeanValidators;
import com.gqj.meter.domain.MdStorageAreaInfo;
import com.gqj.meter.mapper.MdStorageAreaInfoMapper;
import com.gqj.meter.service.IMdStorageAreaInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;
import java.util.Map;

/**
 * 存放区信息Service业务层处理
 *
 * @author lxq
 * @date lxq-01-09
 */
@Service
public class MdStorageAreaInfoServiceImpl implements IMdStorageAreaInfoService
{
    @Autowired
    private MdStorageAreaInfoMapper mdStorageAreaInfoMapper;
    @Autowired
    protected Validator validator;
    private static final Logger log = LoggerFactory.getLogger(MdStorageAreaInfoServiceImpl.class);

    /**
     * 查询存放区信息
     *
     * @param storeAreaId 存放区信息主键
     * @return 存放区信息
     */
    @Override
    public MdStorageAreaInfo selectMdStorageAreaInfoByStoreAreaId(Long storeAreaId)
    {
        return mdStorageAreaInfoMapper.selectMdStorageAreaInfoByStoreAreaId(storeAreaId);
    }

    /**
     * 查询存放区信息列表
     *
     * @param mdStorageAreaInfo 存放区信息
     * @return 存放区信息
     */
    @Override
    public List<MdStorageAreaInfo> selectMdStorageAreaInfoList(MdStorageAreaInfo mdStorageAreaInfo)
    {
        return mdStorageAreaInfoMapper.selectMdStorageAreaInfoList(mdStorageAreaInfo);
    }

    /**
     * 新增存放区信息
     *
     * @param mdStorageAreaInfo 存放区信息
     * @return 结果
     */
    @Override
    public int insertMdStorageAreaInfo(MdStorageAreaInfo mdStorageAreaInfo)
    {

        return mdStorageAreaInfoMapper.insertMdStorageAreaInfo(mdStorageAreaInfo);
    }

    /**
     * 修改存放区信息
     *
     * @param mdStorageAreaInfo 存放区信息
     * @return 结果
     */
    @Override
    public int updateMdStorageAreaInfo(MdStorageAreaInfo mdStorageAreaInfo)
    {
        return mdStorageAreaInfoMapper.updateMdStorageAreaInfo(mdStorageAreaInfo);
    }

    /**
     * 批量删除存放区信息
     *
     * @param storeAreaIds 需要删除的存放区信息主键
     * @return 结果
     */
    @Override
    public int deleteMdStorageAreaInfoByStoreAreaIds(Long[] storeAreaIds)
    {
        return mdStorageAreaInfoMapper.deleteMdStorageAreaInfoByStoreAreaIds(storeAreaIds);
    }

    /**
     * 删除存放区信息信息
     *
     * @param storeAreaId 存放区信息主键
     * @return 结果
     */
    @Override
    public int deleteMdStorageAreaInfoByStoreAreaId(Long storeAreaId)
    {
        return mdStorageAreaInfoMapper.deleteMdStorageAreaInfoByStoreAreaId(storeAreaId);
    }

    /**
     * 校验存放区编号是否唯一
     *
     * @param mdStorageAreaInfo 存放区信息
     * @return 结果
     */
    @Override
    public String checkStoreAreaNoUnique(MdStorageAreaInfo mdStorageAreaInfo) {
        Long storeAreaId = StringUtils.isNull(mdStorageAreaInfo.getStoreAreaId()) ? -1L : mdStorageAreaInfo.getStoreAreaId();
        MdStorageAreaInfo info = mdStorageAreaInfoMapper.checkStoreAreaNoUnique(mdStorageAreaInfo.getStoreAreaNo());
        if (StringUtils.isNotNull(info) && info.getStoreAreaId().longValue() != storeAreaId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询存放区下拉框
     *
     * @param mdStorageAreaInfo 存放区
     * @return 存放区集合
     */
    public List<Map<String, Object>> mdStorageAreaInfoOptions(MdStorageAreaInfo mdStorageAreaInfo){
        return mdStorageAreaInfoMapper.mdWhAreaInfoOptions(mdStorageAreaInfo);
    }


    /**
     * 导入存放区数据
     *
     * @param mdStorageAreaInfoList 存放区列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    public String importMdStorageAreaInfo(List<MdStorageAreaInfo> mdStorageAreaInfoList, Boolean isUpdateSupport, String operName){
        if (StringUtils.isNull(mdStorageAreaInfoList) || mdStorageAreaInfoList.size() == 0) {
            throw new ServiceException("导入存放区信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MdStorageAreaInfo storageAreaInfo : mdStorageAreaInfoList) {
            try {
                BeanValidators.validateWithException(validator, storageAreaInfo);
                // 验证是否存在这个数据
                MdStorageAreaInfo w = mdStorageAreaInfoMapper.selectMdStorageAreaInfoByStoreAreaNo(storageAreaInfo.getStoreAreaNo());
                if (StringUtils.isNull(w)) {
                    //查询当前最大id
                    Long storeAreaId = mdStorageAreaInfoMapper.selectMaxId();
                    if (null == storeAreaId) {
                        storeAreaId = (long) 1;
                    } else {
                        storeAreaId = storeAreaId + 1;
                    }
                    storageAreaInfo.setStoreAreaId(storeAreaId);
                    this.insertMdStorageAreaInfo(storageAreaInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、存放区编号 " + storageAreaInfo.getStoreAreaNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    storageAreaInfo.setStoreAreaId(w.getStoreAreaId());
                    this.updateMdStorageAreaInfo(storageAreaInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、存放区编号 " + storageAreaInfo.getStoreAreaNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、存放区编号 " + storageAreaInfo.getStoreAreaNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、存放区编号 " + storageAreaInfo.getStoreAreaNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
